<?php

class PressTargetVO {
	public $_explicitType = "presstarget.PressTargetVO";
	
	public $idtarget;
	public $day;
	public $time;
	public $length;
	public $note;			
			
	public $section;
			
	public $order_id;		
	public $order_label;
	public $customer_name;
	public $products;
			
	public $press_label;
	public $press_run;
	public $press_sheet;
	public $press_color;
			
	public $paper_label;
	public $paper_thick;    
	
	function PressTargetVO() {	
		$this->section = 4;
    }
  	
	static function getTargets() {
		// get all targets for section "press"
		$sql = "SELECT 
			press_track_new.target.idtarget AS idtarget, 
			press_track_new.target.day AS target_day,
			press_track_new.target.time AS target_time,
			press_track_new.target.length AS target_length,
			press_track_new.target.note AS target_note,
			press_track_new.target.task AS task_id,
			press_track_new.task.order AS order_id,
			press_track_new.task.execute AS execute,
			press_track_new.task.label AS task_label,
			press_track_new.task.value AS task_value,
			press_track_new.task.section AS task_section
			FROM press_track_new.target, press_track_new.task					
			WHERE press_track_new.task.idtask = press_track_new.target.task
			AND press_track_new.task.section = 4";			 
		$query = mysql_query($sql);
		if (mysql_errno()) return mysql_errno() . ": " . mysql_error();
		
		while ($row = mysql_fetch_object($query)) 
		{
			// get order's label
			$sql = "SELECT press_track_new.order.label AS order_label, user.name AS customer_name
			FROM press_track_new.order, press_track_new.user 
			WHERE press_track_new.order.idorder = $row->order_id
			AND iduser = customer";
			$query2 = mysql_query($sql);
			if (mysql_errno()) return mysql_errno() . ": " . mysql_error();			
			$_order = mysql_fetch_object($query2);			
			$row->order_label = $_order->order_label;
			$row->customer_name = $_order->customer_name;
			
			// get products
			$sql = "SELECT idproduct, label, run FROM product WHERE idproduct IN (SELECT product_id FROM relation WHERE task_id = $row->task_id)";
			$query2 = mysql_query($sql);
			if (mysql_errno()) return mysql_errno() . ": " . mysql_error();	
			
			while ($pr = mysql_fetch_object($query2)) {
				$row->products .= $pr->label . "(" . $pr->run . ")" . ";" ;
				$idproduct = $pr->idproduct;
			}
			
			// get paper task
			$sql = "SELECT value FROM task 
				WHERE label = 'бумага'
				AND idtask IN (SELECT task_id FROM relation WHERE product_id = $idproduct)";
			$query3 = mysql_query($sql);
			if (mysql_errno()) return mysql_errno() . ": " . mysql_error();	
			
			while ($tsk = mysql_fetch_object($query3)) {
				$row->paper = $tsk->value;
			}
			
			// create target
			$target = new PressTargetVO();
			$target->idtarget		= $row->idtarget;
			$target->day 			= $row->target_day;
			$target->time 			= $row->target_time;
			$target->length			= $row->target_length;
			$target->note			= $row->target_note;			
			
			$target->order_id		= $row->order_id;
			$target->order_label	= $row->order_label;
			$target->customer_name	= $row->customer_name;
			$target->products		= $row->products;
			
			$temp = explode("\r", $row->task_value);
			$target->press_label	= trim( str_replace("машина : ", 		"", $temp[0]) );
			$target->press_run		= trim( str_replace("тираж : ", 		"", $temp[1]) );
			$target->press_sheet	= trim( str_replace("формат печати : ", "", $temp[3]) );
			$target->press_color	= trim( str_replace("красочность : ", 	"", $temp[4]) );
			
			$temp = explode("\r", $row->paper);
			$target->paper_label	= trim( str_replace("название : ", 		"", $temp[0]) );
			$target->paper_thick	= trim( str_replace("плотность : ", 	"", $temp[1]) );
			
			// targets
			$sql = "SELECT idtarget, day, time, length, note, task, label, section 
				FROM press_track_new.target, press_track_new.task 
				WHERE target.idtarget != $target->idtarget AND target.task = task.idtask AND task.order = $target->order_id ORDER BY section";
			$query_targets = mysql_query($sql);		
			if (mysql_errno()) return mysql_errno() . " : " . mysql_error();		
		
			while ($res_target = mysql_fetch_object($query_targets)) 
			{
				$target->targets[] = $res_target;
			}
			
			$ArrayOfTargets[] = $target;
			$target = null;
		}
		return $ArrayOfTargets;
	}	
}
?>